/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainJFrame.java
 *
 * Created on 26-oct-2012, 14:46:27
 */
package views;

import IA.DistFS.Servers.WrongParametersException;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import serv.Servi;

/**
 *
 * @author ferran
 */
public class MainJFrame extends javax.swing.JFrame {

	private JDialog dialog;
	
	/** Creates new form MainJFrame */
	public MainJFrame() {
		initComponents();
		
		this.dialog = new JDialog (this, true);
        this.dialog.setTitle("Simulated Annealing Params");
        this.dialog.add(new AnnealingParamPanel());
        this.dialog.setLocationRelativeTo(null);
        this.dialog.setSize(275, 210);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        buttonGroup4 = new javax.swing.ButtonGroup();
        buttonGroup5 = new javax.swing.ButtonGroup();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        fitxers = new javax.swing.JSpinner();
        users = new javax.swing.JSpinner();
        repliques = new javax.swing.JSpinner();
        servers = new javax.swing.JSpinner();
        jLabel7 = new javax.swing.JLabel();
        peticions = new javax.swing.JButton();
        seeds = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        escenari = new javax.swing.JCheckBox();
        objectiu_temps = new javax.swing.JRadioButton();
        objectiu_temps_equilibri = new javax.swing.JRadioButton();
        jPanel4 = new javax.swing.JPanel();
        alg_hillclimbing = new javax.swing.JRadioButton();
        alg_simulatedannealing = new javax.swing.JRadioButton();
        sa_params = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        inicial_random = new javax.swing.JRadioButton();
        inicial_inteligent = new javax.swing.JRadioButton();
        jPanel6 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        analisis = new javax.swing.JSpinner();
        executar = new javax.swing.JButton();
        jPanel7 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        resultat = new javax.swing.JTextArea();
        copy = new javax.swing.JButton();

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("IA - Búsqueda Local");

        jLabel6.setFont(new java.awt.Font("Verdana", 1, 36));
        jLabel6.setText("Búsqueda Local");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Paràmetres inicials", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Grande", 1, 18))); // NOI18N

        jLabel1.setText("Número d'usuaris: ");

        jLabel2.setText("Fitxers màxims per usuari: ");

        jLabel3.setText("Número de servidors: ");

        jLabel5.setText("Repliques mínimes per fitxer: ");

        fitxers.setValue(Servi.maxFitxers);
        fitxers.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                fitxersStateChanged(evt);
            }
        });

        users.setValue(Servi.usuarios);
        users.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                usersStateChanged(evt);
            }
        });

        repliques.setValue(Servi.minRepFitx);
        repliques.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                repliquesStateChanged(evt);
            }
        });

        servers.setValue(Servi.maxServers);
        servers.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                serversStateChanged(evt);
            }
        });

        jLabel7.setText("Seeds: ");

        peticions.setText("Generar Peticions");
        peticions.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                peticionsActionPerformed(evt);
            }
        });

        seeds.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "915", "350", "463", "526", "331", "520", "843", "363", "405", "205" }));

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(jLabel2))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(fitxers)
                            .add(users, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE))
                        .add(207, 207, 207)
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel3)
                            .add(jLabel5)))
                    .add(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(peticions, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 230, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(141, 141, 141)
                        .add(jLabel7)))
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(51, 51, 51)
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(repliques)
                            .add(servers, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)))
                    .add(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(seeds, 0, 129, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(28, 28, 28)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(servers, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel3)
                    .add(users, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1))
                .add(16, 16, 16)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel2)
                    .add(fitxers, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel5)
                    .add(repliques, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 17, Short.MAX_VALUE)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jLabel7)
                        .add(seeds, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(peticions, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 38, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Escenari", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Grande", 1, 18))); // NOI18N

        escenari.setSelected(true);
        escenari.setText("Retornar tots els fitxers demanats");
        escenari.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                escenariActionPerformed(evt);
            }
        });

        objectiu_temps.setSelected(true);
        objectiu_temps.setText("Minimitzar temps de descarrega");
        objectiu_temps.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                objectiu_tempsActionPerformed(evt);
            }
        });

        objectiu_temps_equilibri.setText("Minimitzar + Equilibarar temps de descarrega");
        objectiu_temps_equilibri.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                objectiu_temps_equilibriActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(escenari))
                    .add(jPanel3Layout.createSequentialGroup()
                        .add(45, 45, 45)
                        .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(objectiu_temps_equilibri)
                            .add(objectiu_temps))))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(escenari)
                .add(18, 18, 18)
                .add(objectiu_temps)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(objectiu_temps_equilibri)
                .addContainerGap(35, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Algorismes", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Grande", 1, 18))); // NOI18N

        alg_hillclimbing.setSelected(true);
        alg_hillclimbing.setText("Hill Climbing");
        alg_hillclimbing.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alg_hillclimbingActionPerformed(evt);
            }
        });

        alg_simulatedannealing.setText("Simulated Annealing");
        alg_simulatedannealing.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alg_simulatedannealingActionPerformed(evt);
            }
        });

        sa_params.setText("Params");
        sa_params.setEnabled(false);
        sa_params.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sa_paramsActionPerformed(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        jLabel8.setText("Seleccionar estat inicial:");

        inicial_random.setText("Ràpida");
        inicial_random.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inicial_randomActionPerformed(evt);
            }
        });

        inicial_inteligent.setSelected(true);
        inicial_inteligent.setText("Inteligent");
        inicial_inteligent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inicial_inteligentActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel5Layout = new org.jdesktop.layout.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel5Layout.createSequentialGroup()
                .add(jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel5Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabel8))
                    .add(jPanel5Layout.createSequentialGroup()
                        .add(33, 33, 33)
                        .add(jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(inicial_inteligent)
                            .add(inicial_random))))
                .addContainerGap(188, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel5Layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jLabel8)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(inicial_random)
                .add(7, 7, 7)
                .add(inicial_inteligent))
        );

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(alg_hillclimbing)
                    .add(jPanel4Layout.createSequentialGroup()
                        .add(alg_simulatedannealing)
                        .add(18, 18, 18)
                        .add(sa_params, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 162, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jPanel5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(37, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .add(16, 16, 16)
                .add(alg_hillclimbing)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(alg_simulatedannealing)
                    .add(sa_params))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Execució", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Grande", 1, 18))); // NOI18N

        jLabel9.setText("Número d'anàlisis a realitzar: ");

        analisis.setValue(10);

        executar.setText("Executar");
        executar.setEnabled(false);
        executar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                executarActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel6Layout = new org.jdesktop.layout.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel6Layout.createSequentialGroup()
                        .add(jLabel9)
                        .add(18, 18, 18)
                        .add(analisis, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 66, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(executar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 216, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(532, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel6Layout.createSequentialGroup()
                .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jLabel9, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(analisis))
                .add(18, 18, 18)
                .add(executar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 43, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(24, Short.MAX_VALUE))
        );

        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Resultats", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Grande", 1, 18))); // NOI18N

        resultat.setColumns(20);
        resultat.setRows(5);
        resultat.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                resultatFocusGained(evt);
            }
        });
        jScrollPane1.setViewportView(resultat);

        copy.setText("Copiar");
        copy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                copyActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel7Layout = new org.jdesktop.layout.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
                    .add(copy))
                .addContainerGap())
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel7Layout.createSequentialGroup()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
                .add(18, 18, 18)
                .add(copy)
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabel4)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jPanel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(18, 18, 18)
                        .add(jPanel7, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(297, 297, 297)
                        .add(jLabel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 364, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jLabel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(20, 20, 20)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel7, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(98, 98, 98)
                                .add(jLabel4))
                            .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jPanel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(68, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void peticionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_peticionsActionPerformed
		// TODO add your handling code here:
		this.executar.setEnabled(false);
		
		//mirar buits i fer random:
		Random r = new Random();
		if ( (Integer)this.users.getValue() == 0 ) this.users.setValue( r.nextInt(Servi.RandomMax) );
		if ( (Integer)this.servers.getValue() == 0 ) this.servers.setValue( r.nextInt(Servi.RandomMax) );
		if ( (Integer)this.fitxers.getValue() == 0 ) this.fitxers.setValue( r.nextInt(Servi.RandomMax) );
		if ( (Integer)this.repliques.getValue() == 0 ) {
			int maxRepliques = (int)((Integer)this.servers.getValue()*0.5);
			this.repliques.setValue( r.nextInt(maxRepliques) );
		}
		//if ( (Integer)this.seed.getValue() == 0 ) this.seed.setValue( r.nextInt() );
		
		
		//Comprovar dades:
		if ( (Integer)this.repliques.getValue() >  (int)((Integer)this.servers.getValue()*0.5) ) {
			//TODO
		}
		
		
		//Pillar valors i guardar-los a Servi:
		Servi.usuarios = (Integer)this.users.getValue();
		Servi.maxServers = (Integer)this.servers.getValue();
		Servi.maxFitxers = (Integer)this.fitxers.getValue();
		Servi.minRepFitx = (Integer)this.repliques.getValue();
		Servi.seeds = (Integer) Servi.vSeeds[ this.seeds.getSelectedIndex() ];
		
		try {
			
			//Genrar peticions
			Servi.generatePeticions();
			this.executar.setEnabled(true);
			
		} catch (WrongParametersException ex) {
			Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex);
		}
	}//GEN-LAST:event_peticionsActionPerformed

	private void usersStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_usersStateChanged
		// TODO add your handling code here:
		this.executar.setEnabled(false);
	}//GEN-LAST:event_usersStateChanged

	private void escenariActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_escenariActionPerformed
		// TODO add your handling code here:
		if ( this.escenari.isSelected() ) {
			this.objectiu_temps.setEnabled(true);
		}
		else {
			this.objectiu_temps.setEnabled(false);
			if ( this.objectiu_temps.isSelected() ) {
				this.objectiu_temps_equilibri.setSelected(true);
				this.objectiu_temps.setSelected(false);
			}
		}
	}//GEN-LAST:event_escenariActionPerformed

	private void objectiu_tempsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_objectiu_tempsActionPerformed
		// TODO add your handling code here:
		boolean b = !this.objectiu_temps_equilibri.isSelected();
		this.objectiu_temps_equilibri.setSelected(b);
	}//GEN-LAST:event_objectiu_tempsActionPerformed

	private void objectiu_temps_equilibriActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_objectiu_temps_equilibriActionPerformed
		// TODO add your handling code here:
		if ( !this.escenari.isSelected() ) {
			this.objectiu_temps_equilibri.setSelected(true);
		}
		else {
			boolean b = !this.objectiu_temps.isSelected();
			this.objectiu_temps.setSelected(b);
		}
		
	}//GEN-LAST:event_objectiu_temps_equilibriActionPerformed

	private void alg_hillclimbingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alg_hillclimbingActionPerformed
		// TODO add your handling code here:
		boolean b = !this.alg_simulatedannealing.isSelected();
		this.alg_simulatedannealing.setSelected(b);
		this.sa_params.setEnabled(b);
	}//GEN-LAST:event_alg_hillclimbingActionPerformed

	private void executarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_executarActionPerformed
		// TODO add your handling code here:
		int a = (Integer)this.analisis.getValue();
		if ( a > 0 ) {
			
			//GUARDAR PARAMETRES
			
			//Escenari
			if ( this.escenari.isSelected() ) Servi.esc = Servi.Escenari.retornatots;
			else Servi.esc = Servi.Escenari.noretornatots;
			
			//Cas
			if ( this.objectiu_temps.isSelected() ) Servi.cas = Servi.Casos.cas1;
			else Servi.cas = Servi.Casos.cas2;
			
			//Algorithme
			if ( this.alg_hillclimbing.isSelected() ) Servi.current = Servi.Algorithm.HillClimbing;
			else Servi.current = Servi.Algorithm.SimulatedAnnealing;
			
			//Estat inicial
			if ( this.inicial_inteligent.isSelected() ) Servi.SolucioInicialInteligent = true;
			else Servi.SolucioInicialInteligent = false;
			
			//Executar
			if ( Servi.SortidaR ) this.resultat.setText("Exceució\tSolució\n");
			for ( int i = 0; i < a ; i++ ) {
				try {
					String res = Servi.execute();
					this.resultat.append(res + "\n");
				} catch (Exception ex) {
					Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex);
				}
			}
			
		}
	}//GEN-LAST:event_executarActionPerformed

	private void alg_simulatedannealingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alg_simulatedannealingActionPerformed
		// TODO add your handling code here:
		boolean b = !this.alg_hillclimbing.isSelected();
		this.alg_hillclimbing.setSelected(b);
		this.sa_params.setEnabled(!b);
	}//GEN-LAST:event_alg_simulatedannealingActionPerformed

	private void inicial_randomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inicial_randomActionPerformed
		// TODO add your handling code here:
		boolean b = !this.inicial_inteligent.isSelected();
		this.inicial_inteligent.setSelected(b);
	}//GEN-LAST:event_inicial_randomActionPerformed

	private void inicial_inteligentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inicial_inteligentActionPerformed
		// TODO add your handling code here:
		boolean b = !this.inicial_random.isSelected();
		this.inicial_random.setSelected(b);
	}//GEN-LAST:event_inicial_inteligentActionPerformed

	private void fitxersStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_fitxersStateChanged
		// TODO add your handling code here:
		this.executar.setEnabled(false);
	}//GEN-LAST:event_fitxersStateChanged

	private void serversStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_serversStateChanged
		// TODO add your handling code here:
		this.executar.setEnabled(false);
	}//GEN-LAST:event_serversStateChanged

	private void repliquesStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_repliquesStateChanged
		// TODO add your handling code here:
		this.executar.setEnabled(false);
	}//GEN-LAST:event_repliquesStateChanged

	private void sa_paramsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sa_paramsActionPerformed
		// TODO add your handling code here:
		this.dialog.setAlwaysOnTop(true);
        this.dialog.setLocationRelativeTo(this);
        this.dialog.setVisible(true);
	}//GEN-LAST:event_sa_paramsActionPerformed

	private void resultatFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_resultatFocusGained

		// TODO add your handling code here:}//GEN-LAST:event_resultatFocusGained

		private void copyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyActionPerformed
		// TODO add your handling code here:
		String textocopiado = this.resultat.getText(); 
		//Copiar al Portapapeles    
		StringSelection data = new StringSelection( textocopiado );
		Clipboard clipboard = getToolkit().getSystemClipboard(); //Incializa variable
		clipboard.setContents(data, data); // Coloca dato al portapapeles
	}//GEN-LAST:event_copyActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
		 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		//</editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				new MainJFrame().setVisible(true);
			}
		});
	}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton alg_hillclimbing;
    private javax.swing.JRadioButton alg_simulatedannealing;
    private javax.swing.JSpinner analisis;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.ButtonGroup buttonGroup4;
    private javax.swing.ButtonGroup buttonGroup5;
    private javax.swing.JButton copy;
    private javax.swing.JCheckBox escenari;
    private javax.swing.JButton executar;
    private javax.swing.JSpinner fitxers;
    private javax.swing.JRadioButton inicial_inteligent;
    private javax.swing.JRadioButton inicial_random;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton objectiu_temps;
    private javax.swing.JRadioButton objectiu_temps_equilibri;
    private javax.swing.JButton peticions;
    private javax.swing.JSpinner repliques;
    private javax.swing.JTextArea resultat;
    private javax.swing.JButton sa_params;
    private javax.swing.JComboBox seeds;
    private javax.swing.JSpinner servers;
    private javax.swing.JSpinner users;
    // End of variables declaration//GEN-END:variables
}
